package com.warningrc.module.user.struts.action;

import java.net.URLDecoder;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.warningrc.module.user.service.UserService;
import com.warningrc.system.Constants;

public class UserLoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	private String url;
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login() throws Exception {
		String userId = userService.login(getUserName(), getPassword());
		if (userId == null) {
			return LOGIN;
		} else {
			ActionContext.getContext().getSession().put(
					Constants.USER_LOGINED_MESSAGE, userId);
			if (getUrl() != null && getUrl().trim().length() != 0) {
				setUrl(URLDecoder.decode(getUrl(), "utf-8"));
				return "returnUrl";
			} else
				return "index";
		}
	}

	public String logout() {
		return SUCCESS;
	}
}
